﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Windows.Forms;

namespace Demo_HighSpeed
{
    public partial class Frm_HighSpeed : Form
    {
        byte[] imgData;
        int imgW;
        int imgH;
        int total = 0;
        Thread thShow;
        private ManualResetEvent evtShow = new ManualResetEvent(false);
        System.Threading.Timer timer;
        Random random = new Random();
        public Frm_HighSpeed()
        {
            InitializeComponent();
            snsPictureBox1.IsUseOpenGL = true;//使用OpenGl进行图像显示
            snsPictureBox1.IsSawtooth = true;//开启后像素颗粒感比较强
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //创建图像数据并给图像涂上背景色
            imgW = 2160;
            imgH = 1920;
            imgData = new byte[imgW * imgH * 3];
            for (int i = 0; i < imgH; i++)
            {
                for (int j = 0; j < imgW; j++)
                {
                    imgData[(i * imgW + j) * 3] = 10;//b
                    imgData[(i * imgW + j) * 3 + 1] = 8;//g
                    imgData[(i * imgW + j) * 3 + 2] = 8;//r
                }
            }
            //开启一个定时器，用于图像显示信号的输出
            timer = new System.Threading.Timer(new System.Threading.TimerCallback(TimerEvent), null, 0, 10);//单位ms
            //创建专门用于图像显示的线程
            thShow = new Thread(ShowImage);
            thShow.IsBackground = true;
            thShow.Start();
        }
        private void TimerEvent(object a)
        {
            //记录当前图像的数量
            total++;
            //给图像显示线程发送信号
            evtShow.Set();
        }

        private void ShowImage()
        {
           //Bitmap bitmap = new Bitmap(@"D:\搜狗高速下载\2K4K\4K.jpg");
           // BitmapData mapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
           // bitmap.UnlockBits(mapData);
           // imgData = new byte[mapData.Stride * mapData.Height];
           // System.Runtime.InteropServices.Marshal.Copy(mapData.Scan0, imgData, 0, imgData.Length);
            while (true)
            {
                evtShow.WaitOne();//等待图像显示信号
                int x = random.Next(10, imgW - 10);
                int y = random.Next(10, imgH - 10);
                int r = random.Next(0, 255);
                int g = random.Next(0, 255);
                int b = random.Next(0, 255);

                imgData[(y * imgW + x) * 3] = (byte)b;
                imgData[(y * imgW + x) * 3 + 1] = (byte)g;
                imgData[(y * imgW + x) * 3 + 2] = (byte)r;

                snsPictureBox1.ShowImage(imgData, imgW, imgH, PixelFormat.Format24bppRgb);
                //snsPictureBox1.ShowImage(imgData, bitmap.Width, bitmap.Height, bitmap.PixelFormat);
                evtShow.Reset();//复原等待状态
            }
        }

        private void snsPictureBox1_Paint(object sender, PaintEventArgs e)
        {
            string strTime = "";
            strTime += string.Format("Size:{0}x{1}", snsPictureBox1.ImgWidth, snsPictureBox1.ImgHeight);
            strTime += string.Format("     Scale:{0:F2}", snsPictureBox1.SnsScale);
            strTime += string.Format("     ShowFps:{0:F1}", snsPictureBox1.ShowFps);
            strTime += string.Format("     ImgId:{0}", total);
            snsPictureBox1.ShowString(strTime, new PointF(snsPictureBox1.XSpace + 1, snsPictureBox1.YSpace + 1), false);
        }

        private void snsPictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            snsPictureBox1.Init();
        }

    }
}
